#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# - This file is for making updated .ui file in starting
# - It has a small md5 checker for .ui file, .log keeps that number
# 
#  Taha Doğan Güneş
#   - gfh-araci
#   - GPL-v3
#


from PyQt4.QtGui import *
from PyQt4.QtCore import *

class RunApplication:
    def __init__(self):
        try:
            self.comparefiles()
            self.run()
        except ImportError:
            self.firstrun()
        except IOError: #This means no log file is found!
            self.firstrun()
    def run(self):
        from main import MainWindow
        import sys, os


        print "gfh-araci is started!"
        app = QApplication(sys.argv)
        main_window = MainWindow()
        main_window.show()
        sys.exit(app.exec_())

    def firstrun(self): #Thanks Gökmen Görgen's puding
        import glob
        import os
 
        
        ui_files = glob.glob("./*.ui")
 
        for qt_file in ui_files:
            command = "pyuic4"
            file_name_ext = "ui"
 
            qt_file_name = os.path.split(qt_file)[-1]
            py_file_name = os.path.splitext(qt_file_name)[0] + "_%s.py" % file_name_ext
 
            print("Converting %s as %s..." % (qt_file_name, py_file_name))
            #Mac problem solved!
            if os.name == "posix": #No need /usr/bin this should work on linux
                os.system("%s %s -o %s/%s" % \
                    (command, qt_file, os.getcwd(), py_file_name))
            elif os.name == "nt": #NT support. This is not in puding :)
                qt_file = qt_file.split(os.sep)[-1]
                os.system("%s -o %s %s" % \
                    (command, py_file_name, qt_file))
        
        self.checkfiles()
        self.run()

    def checkfiles(self):
        import hashlib
        import os
        import glob
        log_files = glob.glob("./*.log")
        ui_files = glob.glob("./*.ui" )
        pyc_files = glob.glob("./*.pyc")

        print "Deleting log_files and pyc_files..."
        for i in log_files + pyc_files:
            os.remove(i)

        print "Checking ui_files"
        for i in ui_files:
             readfile = open(i).read()
             a = hashlib.md5()
             a.update(readfile)
             number = a.hexdigest()
 
        print "Writing a new log file"
        filehandle = open('ui.log', 'w')
        filehandle.write(number )
        filehandle.close()

    def comparefiles(self):
        import glob
        import os
        import hashlib
        print "Reading ui.log"
        filehandle = open('ui.log')
        a = filehandle.readlines()
        lognumber = a[0]
        filehandle.close()
        
        ui_files = glob.glob("./*.ui" )

        print "Generating md5 number"
        for i in ui_files:
             readfile = open(i).read()
             a = hashlib.md5()
             a.update(readfile)
             number = a.hexdigest()
        
        if lognumber == number:
            print "ui_files are OK."
        else:
            print "ui_files was changed! Returns to the first run..."
            self.firstrun()



